import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import {resolve} from "path";

const pathResolve = (dir: string): any => {
  return resolve(__dirname, dir)
}

export default defineConfig({
  plugins: [vue()],
  resolve: {
    alias: {
      '@': pathResolve('./src')
    }
  },

  build: {
    rollupOptions: {
      output: {
        manualChunks(id) {
          if (id.includes('node_modules')) {
            // 按库名拆分第三方依赖到同一文件中
            return id.toString().split('node_modules/')[1].split('/')[0];
          }
        },
        chunkFileNames: (chunkInfo) => {
          const facadeModuleId = chunkInfo.facadeModuleId?.split('/') || [];
          const fileName = facadeModuleId[facadeModuleId.length - 2] || '[name]';
          return `js/${fileName}/[name].[hash].js`; // 可读性更好的命名规则
        }
      }
    },
    chunkSizeWarningLimit: 1500 // 调整阈值避免误报（单位KB）
  }
})


